<!--
 * @Author: your name
 * @Date: 2020-12-20 10:24:14
 * @LastEditTime: 2021-04-06 01:01:54
 * @LastEditors: Please set LastEditors
 * @Description: 销售出库详情，该页面能点击审核和记账
 * @FilePath: \cang-chu-guan-li-xi-tong\src\views\chukuguanli\XiaoShouChuKuDetail.vue
-->
<template>
  <a-card>
    <p style="font-size: 18px; text-align: center">组装出库单</p>
    <br />
    <a-form-model :model="Form" v-bind="layout"
      ><!-- 顶部的信息  -->

      <a-row>
        <a-col :xl="6" :lg="6" :md="12" :sm="24" :xs="24">
          <a-form-model-item label="单据号">
            <div>{{ Form.document }}</div>
          </a-form-model-item>
        </a-col>

        <a-col :xl="6" :lg="6" :md="12" :sm="24" :xs="24">
          <a-form-model-item label="单据日期">
            <div>{{ Form.document_date }}</div>
          </a-form-model-item>
        </a-col>

        <a-col :xl="6" :lg="6" :md="12" :sm="24" :xs="24">
          <a-form-model-item label="组装费用">
            <div>{{ Form.assemblyTotalCost }}</div>
          </a-form-model-item>
        </a-col>

        <a-col :xl="6" :lg="6" :md="12" :sm="24" :xs="24">
          <a-form-model-item label="供应商">
            <div>{{ Form.supplier }}</div>
          </a-form-model-item>
        </a-col>
      </a-row>

      <!-- <a-col :xl="6" :lg="6" :md="12" :sm="24" :xs="24">
        <a-form-model-item label="备注">
          <div>{{ Form.remarks }}</div>
        </a-form-model-item>
      </a-col> -->
      <a-col :xl="6" :lg="6" :md="12" :sm="24" :xs="24">
        <a-form-model-item label="库存组织">
          <div>{{ Form.pkCalbody_dictText }}</div>
        </a-form-model-item>
      </a-col>
      <a-col :xl="6" :lg="6" :md="12" :sm="24" :xs="24">
        <a-form-model-item label="公司">
          <div>{{ Form.pkStordoc_dictText }}</div>
        </a-form-model-item>
      </a-col>
    </a-form-model>

    <a-table
      :columns="columns"
      :data-source="DataSource"
      style="margin-top: 10px"
      :pagination="false"
      :scroll="autoSize"
    >
    </a-table>

    <div style="margin-top: 20px">
      <!-- 单据 -->
      <a-form-model :model="Form" v-bind="layout"
        ><!-- 顶部的信息 -->
        <a-row>
          <!-- <a-col :xl="6" :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="负责人">
              <div>{{ Form.responsiblePerson }}</div>
            </a-form-model-item>
          </a-col> -->

          <a-col :xl="6" :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="审核人">
              <div>{{ Form.inspector }}</div>
            </a-form-model-item>
          </a-col>

          <a-col :xl="6" :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="审核日期">
              <div>{{ Form.balance }}</div>
            </a-form-model-item>
          </a-col>

          <a-col :xl="6" :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="制单人">
              <div>{{ Form.singleProducer }}</div>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>

      <!-- 提交按钮组  
      1. 能审核提交的人是不能点击保存的
      2. 能保存的人是不能审核和提交的
      -->
      <div style="margin-top: 20px">
        <center>
          <!-- <a-button type="primary" style="margin-right: 20px"> 打印 </a-button> -->

          <a-button style="margin-right: 20px" disabled> 保存 </a-button>

          <a-button
            @click="Examine"
            style="margin-right: 20px"
            :disabled="!alreadyCheckTag"
          >
            审核
          </a-button>

          <a-button
            @click="Examine_otherSide"
            style="margin-right: 20px"
            :disabled="alreadyCheckTag"
            >反审核</a-button
          >
          <!-- <a-button
            type="primary"
            @click="Submit"
            style="margin-right: 20px"
            :disabled="!alreadyRecordTag"
          >
            提交
          </a-button> -->

          <a-button
            type="primary"
            @click="save_u8c"
            style="margin-right: 20px"
            
          >
            同步至用友
          </a-button>
        </center>
      </div>
    </div>
  </a-card>
</template>

<script>
import { BeiHuoTongZhiHttp } from "@/services/modules/ChuKuGuanLi/XiaoShouChuKu/BeiHuoTongZhi";
const bhtzjHttp = new BeiHuoTongZhiHttp(); // 备货通知
import moment from "moment";
import axios from "axios";

let columns = [
  {
    title: "序号",
    dataIndex: "key",
    key: "key",
    align: "center",
    width: "80px",
  },
  {
    title: "组装类型",
    key: "type_dictText",
    dataIndex: "type_dictText",
    align: "center",
  },
  {
    title: "仓库编码",
    key: "depotNo",
    dataIndex: "depotNo",
    align: "center",
  },
  {
    title: "仓库名称",
    key: "depotId_dictText",
    dataIndex: "depotId_dictText",
    align: "center",
  },

  // {
  //   title: "库位",
  //   key: "depotLocation_dictText",
  //   dataIndex: "depotLocation_dictText",
  //   align: "center",
  // },

  {
    title: "仓位",
    key: "position_dictText",
    dataIndex: "position_dictText",
    align: "center",
  },
  {
    title: "存货编码",
    dataIndex: "inventoryNo",
    key: "inventoryNo",
    align: "center",
  },
  {
    title: "存货名称",
    dataIndex: "inventoryId_dictText",
    key: "inventoryId_dictText",
    align: "center",
  },
  {
    title: "规格型号",
    dataIndex: "specificationsModel",
    key: "specificationsModel",
    align: "center",
  },
  {
    title: "单位",
    key: "unit",
    dataIndex: "unit",
    align: "center",
  },
  {
    title: "数量",
    key: "number",
    dataIndex: "number",
    align: "center",
  },
  // {
  //   title: "金额",
  //   key: "money",
  //   dataIndex: "money",
  //   align: "center",
  // },
  {
    title: "收发类别",
    key: "sendAndReceiveCategory_dictText",
    dataIndex: "sendAndReceiveCategory_dictText",
    align: "center",
  },

  {
    title: "批次号",
    key: "batchNumber",
    dataIndex: "batchNumber",
    align: "center",
  },
  {
    title: "转库日期",
    key: "transferDate",
    dataIndex: "transferDate",
    align: "center",
  },
  {
    title: "备注",
    dataIndex: "remark",
    key: "remark",
    align: "center",
  },
];
export default {
  data() {
    return {
      u8c_array: [], //u8c传递数据表格的数据源
      arrKit: [], // 套件
      arrParts: [], //配件
      GeneralBillVO_46: {}, //需给用友传递的data
      GeneralBillVO_46In: [], //需给用友传递的data
      ayyanyany: [],
      ayyanyanyIn: [],
      updateSucessRecordTag: false, //同步至u8c按钮的状态

      Form: {
        id: "", // id
        document: "", // 单据号
        document_date: "", // 单据日期
        depotName: "", // 仓库名称
        depotNo: "", // 仓库编号
        depotId: "", // 仓库Id
        storageUnit: "", // 所属单位名称
        storageUnitId: "", // 所属单位Id
        storageUnitNo: "", // 所属单位编号
        department: "", // 部门名称
        deptId: "", // 部门id
        client: "", // 客户名称
        clientId: "", // 客户Id
        sendReceiveType: "", // 收发类别名称
        sendReceiveTypeId: "", // 收发类别Id
        responsiblePerson: "", // 负责人名称
        responsiblePersonId: "", // 负责人Id
        inspector: "", // 审核人名称
        inspectorId: "", // 审核人Id
        balance: "", // 审核日期
        singleProducer: "", // 制单人名称
        singleProducerId: "", // 制单人Id
      },
      // 格式
      layout: {
        labelCol: { span: 8 },
        wrapperCol: { span: 15 },
      },

      columns,
      DataSource: [],
      autoSize: {
        x: 3500,
        y: 670,
      },
      alreadyCheckTag: true, // 可以审核
      alreadyRecordTag: true, // 可以记账
    };
  },
  watch: {
    $route: function () {
      this.getRouterParam();
    },
  },
  mounted() {
    this.getRouterParam();
  },

  methods: {
    //反审核函数
    Examine_otherSide() {
      let data = {
        id: this.Form.id,
        auditMark: 2,
      };
      // 提交审核信息，修改表一数据
      bhtzjHttp.updataAssemblyOrder(data).then((res) => {
        if (res.code == 200) {
          this.$message.success("反审核成功");
          this.$router.push({
            path: "AssembleList",
          });
        } else {
          this.$message.success("反审核失败");
        }
      });
    },
    // 审核函数
    Examine() {
      this.Form.balance = moment().format("YYYY-MM-DD");
      console.log(this.Form.balance);
      let data = {
        id: this.Form.id,
        auditMark: 1,
        reviewerDate: this.Form.balance,
      };
      console.log("要修改的数据:::", data);
      // 提交审核信息，修改表一数据
      bhtzjHttp.updataAssemblyOrder(data).then((res) => {
        if (res.code == 200) {
          this.$message.success("审核成功");
          this.alreadyCheckTag = false;
        } else {
          this.$message.success("审核失败");
        }
      });
    },

    // 提交
    Submit() {
      let data = {
        id: this.Form.id,
        bookkeepingMark: 1,
      };
      // 提交审核信息，修改表一数据
      bhtzjHttp.updataAssemblyOrder(data).then((res) => {
        if (res.code == 200) {
          this.$message.success("记账成功");
          this.alreadyRecordTag = false;
        } else {
          this.$message.success("记账失败");
        }
      });
    },

    // 单据同步至u8c
    save_u8c() {
      this.ayyanyany = [];
      // ==========组装=========
      let _data = this.DataSource;
      if (_data) {
        _data.forEach((item) => {
          console.log("item", item);
          if (item.type_dictText == "套件") {
            this.arrKit.push(item);
          } else if (item.type_dictText == "配件") {
            this.arrParts.push(item);
          }
        });
      }
      console.log("arrKit", this.arrKit);
      console.log("arrParts", this.arrParts);
      this.u8c_array = [];
      // 套件
      this.arrKit.forEach((item) => {
        let data = {
          cinventoryid: item.invcode, //存货
          cwarehouseid: item.storcode, //仓库
          dshldtransnum: item.number, //数量
          fbillrowflag: "0", //类型
        };
        this.u8c_array.push(data);
      });
      // 配件
      this.arrParts.forEach((item) => {
        let data = {
          cinventoryid: item.invcode, //存货√
          cwarehouseid: item.storcode, //仓库√
          dshldtransnum: item.number, //数量√
          fbillrowflag: "1", //类型√
        };
        this.u8c_array.push(data);
      });

      this.GeneralBillVO_46 = {
        parentvo: {
          coperatorid: _data[0].preparedBy1_dictText, //制单人
          dbilldate: moment(_data[0].documentTime).format("YYYY-MM-DD"), //单据日期
          pk_corp: "1001", //公司
          vbillcode: _data[0].document, //单据号
          nfixdisassemblymny: _data[0].assemblyCost, //组装费
          cbilltypecode: "4L", //单据类型
          vuserdef3: _data[0].farmer1_dictText, //!自定义项3 农户
          vnote: _data[0].remark, // 表头备注
        },
        childrenvo: this.u8c_array,
      };
      this.ayyanyany.push(this.GeneralBillVO_46);
      console.log("_data.auditMark_dictText", _data.auditMark_dictText);
      if (_data[0].auditMark_dictText == "已审核") {
        // u8c接口
        axios({
          headers: {
            "Access-Control-Allow-Origin": "*",
            trantype: "code",
            "Content-Type": "application/json",
            usercode: "13355262265",
            password: "5ef208ee90e5504a7b569ebbd6342634",
            system: "test",
            needStackTrace: "Y",
          },
          method: "post",
          url: "/api/u8cloud/api/ic/special4l/save/sign",
          data: {
            specialbillvo: this.ayyanyany,
          },
        }).then((response) => {
          console.log("返回的数据为::::", response.data);
          if (response.data.status == "success") {
            this.$message.success("组装单同步成功");
            this.updateSucessRecordTag = true;
          } else {
            this.$message.error(response.data.errormsg);
          }
        });
      } else {
        this.$message.error("请先审核，再进行同步！");
      }
    },

    // 获取路由跳转参数
    getRouterParam() {
      console.log("查询参数:::", this.$route.query.Id);

      // 通过出入库id 查询出入库信息
      if (typeof this.$route.query.Id == "undefined") {
        console.log("常规跳转");
      } else {
        this.setInitInfo(this.$route.query.Id);
      }
    },

    // 路由跳转,设置初始化信息
    setInitInfo(id) {
      bhtzjHttp.getAssemblyOrderAndGoodsList("?id=" + id).then((res) => {
        console.log("通过Id查询出来的信息::::", res);

        this.DataSource = [];

        // 是否审核
        if (res.result.records[0].auditMark == 1) {
          this.alreadyCheckTag = false;
        } else {
          this.alreadyCheckTag = true;
        }
        // 是否记账
        if (res.result.records[0].bookkeepingMark == 1) {
          this.alreadyRecordTag = false;
        } else {
          this.alreadyRecordTag = true;
        }

        this.otherId = res.result.records[0].id; // 表一 Id

        this.Form = {
          id: res.result.records[0].id, // 表一 Id

          document: res.result.records[0].document, // 单据号
          document_date: res.result.records[0].documentTime, // 单据日期
          assemblyTotalCost: res.result.records[0].assemblyCost, // 组装费用
          supplier: res.result.records[0].farmer_dictText, // 供应商

          // responsiblePerson: res.result.records[0]., // 负责人
          inspector: res.result.records[0].reviewer_dictText, // 审核人
          singleProducer: res.result.records[0].preparedBy_dictText, // 制单人
          balance: res.result.records[0].reviewerDate, // 审核日期

          pkStordoc_dictText: res.result.records[0].pkCorp_dictText, // 公司
          pkCalbody_dictText: res.result.records[0].pkCalbody_dictText, // 库存组织
        };

        for (let i = 0; i < res.result.records.length; i++) {
          let data = {
            key: i + 1,
            id: res.result.records[i].twoId, // 表二Id

            type_dictText: res.result.records[i].type_dictText, // 组装类型

            depotId_dictText: res.result.records[i].depotId_dictText, //
            depotNo: res.result.records[i].storcode, // 仓库编码

            // depotId_dictText: res.result.records[i].depotId_dictText, // 仓库名称
            position_dictText: res.result.records[i].position_dictText, // 库位
            depotLocation_dictText:
              res.result.records[i].depotLocation_dictText, // 仓位
            inventoryNo: res.result.records[i].invcode, // 存货编码

            inventoryId_dictText: res.result.records[i].inventoryId_dictText, // 存货名称
            specificationsModel: res.result.records[i].invspec, // 规格型号

            unit: res.result.records[i].measname, // 单位

            number: res.result.records[i].number, // 数量
            money: res.result.records[i].money, // 金额
            sendAndReceiveCategory_dictText:
              res.result.records[i].sendAndReceiveCategory_dictText, // 收发类别

            transferDate: res.result.records[i].transferDate, // 转库日期
            remark: res.result.records[i].remark, // 备注

            // number: res.result.list[i].number, // 数量
          };
          this.DataSource.push(data);
        }
        console.log("表格信息:::::", this.DataSource);
      });
    },
  },
};
</script>

<style>
</style>
